package {
	import com.google.analytics.debug._Style;
	import com.greensock.TimelineMax;
	import com.greensock.TweenMax;
	import com.greensock.easing.Cubic;
	import com.greensock.easing.Elastic;
	import com.greensock.easing.Linear;
	import com.greensock.easing.Quad;
	import com.greensock.easing.RoughEase;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import org.osflash.signals.Signal;
	
	import subumbo.leg.model.Lib;

	public class Character {
		
		public var selected:Signal; 
		public var removed:Signal;
		
		private var _view:MovieClip;
	
		private var _eyeX:Number;
		
		private var _id:int;
		
		private var _timeout:int;
		
		public function Character(view:MovieClip, id:int) {
			_view = view;
			_id = id;
			_view.outline.visible = false;
			_view.eyes.stop();
			_view.eyes.buttonMode = true;
			view.stencil.visible = false;
			selected = new Signal(int);
			removed = new Signal();
			_eyeX = view.eyes.x;
			blink();
		}
		
		private function onOver(e:MouseEvent) :void {
			TweenMax.to(_view.eyes, .8, {  delay:.4, scaleX:1, scaleY:1, ease:Elastic.easeOut});
			TweenMax.to(_view.eyes, .4, {  scaleX:1.6, scaleY:1.6});
		}
		
		private function onMouseDown(e:Event) :void {
			selected.dispatch(_id);
		}
		
		private function onOut(e:Event) :void {
			TweenMax.to(_view.eyes, .2, { scaleX:1, scaleY:1, ease:Cubic.easeInOut});
			
		}
		
		private function blink():void {
			_view.eyes.alpha = 0;
			_timeout = setTimeout(unBlink, 200)
		}
		
		private function unBlink():void {
			_view.eyes.alpha = 1
			_timeout = setTimeout(blink, Math.random() * 20000)
		}
		
		public function set active(val:Boolean):void {
			_view.eyes.buttonMode = val;
			if(val) {
				
				_view.eyes.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
				_view.eyes.addEventListener(MouseEvent.ROLL_OUT, onOut);
				_view.eyes.addEventListener(MouseEvent.ROLL_OVER, onOver);
			}else {
								
				_view.eyes.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
				_view.eyes.removeEventListener(MouseEvent.ROLL_OUT, onOut);
				_view.eyes.removeEventListener(MouseEvent.ROLL_OVER, onOver);
			}
		}
		
		public function success(fx:FX, point:Point, bonus:Boolean):void {
			var s:MovieClip=  Lib.getMovieClip((bonus) ? "SuccessBonus" : "Success");
			s.x = fx.view.mouseX;
			s.y = fx.view.mouseY;
			s.z = 500; 
			var mouse:Point =  point;
			var point:Point = _view.globalToLocal(point);
			fx.view.addChild(s);
			
			var b:Vector3D = new Vector3D(s.x, s.y ,0);
			var c:Vector3D = new Vector3D(512, 285);
			var sx:Number = c.x + c.x - (75 + (b.x - 75) * .5);
			var sy:Number = c.y + c.y - (-26 + (b.y - -26) * .5);
			
	
			
			var tl:TimelineMax = new TimelineMax();
			//TweenMax.to(s, 0.5, {});
			tl.append(TweenMax.to(s, .8, {rotationX:-50, rotationY:-280, rotationZ:-30, ease:Linear.easeNone, bezier:[{x:sx, y:sy, z:-1300}, {x:75, y:26, z:0}]})); //makes my_mc travel through 250,50 and end up at 500,0.
			
			TweenMax.to(s, .5, {scaleX:0,ease:Linear.easeNone, scaleY:0,alpha:0, delay:2})
			setTimeout(function():void {
				tl.timeScale = .05;
				setTimeout(function():void {
					tl.timeScale = 1;
				}, 1500);
			}, 460);
			//TweenMax.to(s, .5, {z:-300, ease:Linear.easeNone, repeat:1, yoyo:true});
			/*TweenMax.to(s, 1, {x: 75,y: 26, ease:Linear.easeNone, y: _view.stage.stageHeight * .5, rotationY:120, onCompleteParams:[s], onComplete:function(s:MovieClip):void {
				TweenMax.to(s, 0.3, {alpha:0})
			}});*/
			//TweenMax.to(s, .1, {alpha:0, delay:.9});
				
		
				
			_view.outline.visible = true;
			_view.outline.alpha = 0;
			_view.eyes.removeEventListener(MouseEvent.ROLL_OVER, onOver);
			TweenMax.killTweensOf(_view.eyes);
			_view.eyes.scaleX = _view.eyes.scaleY = 1;
			_view.eyes.visible = false;
			TweenMax.to(_view.outline, .3, {alpha:1, onComplete:function():void {
				setTimeout(removed.dispatch, 2000);
				TweenMax.to(_view.outline, 1, {alpha:0, delay:1.4,onComplete:function():void {
					_view.outline.visible = false;
					_view.eyes.visible = true;
					_view.eyes.addEventListener(MouseEvent.ROLL_OVER, onOver);
					_view.visible = false;
					
				}})
			}});
			
			TweenMax.killTweensOf(_view.eyes);
			_view.eyes.scaleX = _view.eyes.scaleY = 1;	
		}
		
		public function fail(fx:FX, point:Point):void {
			var s:MovieClip = Lib.getMovieClip("Fail");
			s.x = _view.stage.mouseX;
			s.y = _view.stage.mouseY;
			fx.view.addChild(s);
			
			
			var b:Vector3D = new Vector3D(s.x, s.y ,0);
			var c:Vector3D = new Vector3D(512, 285);
			var sx:Number = c.x + c.x - (75 + (b.x - 75) * .5);
			var sy:Number = c.y + c.y - (-26 + (b.y - -26) * .5);
			
			
			var tl:TimelineMax = new TimelineMax();
			//TweenMax.to(s, 0.5, {});
			var ran:Number = Math.random();
			if(ran > .5) ran = -1;
			else ran = 1;
			tl.append(TweenMax.to(s, .8, {rotationX:50 * ran, rotationY:280, rotationZ:30 * ran, ease:Linear.easeNone, bezier:[{x:sx, y:sy, z:-1300}, {x:75, y:26, z:0}]})); //makes my_mc travel through 250,50 and end up at 500,0.
			
			TweenMax.to(s, .5, {scaleX:0,ease:Linear.easeNone, scaleY:0,alpha:0, delay:2})
			setTimeout(function():void {
				tl.timeScale = .05;
				setTimeout(function():void {
					tl.timeScale = 1;
				}, 1500);
			}, 460);
			//TweenMax.to(s, .5, {z:-300, e
			
			_view.outline.visible = true;
			_view.outline.alpha = 0;
			_view.eyes.removeEventListener(MouseEvent.ROLL_OVER, onOver);
			TweenMax.killTweensOf(_view.eyes);
			_view.eyes.scaleX = _view.eyes.scaleY = 1;
			_view.eyes.visible = false;
			TweenMax.to(_view.outline, .5, {alpha:1, onComplete:function():void {
				TweenMax.to(_view.outline, 2, {alpha:0, onComplete:function():void {
					_view.outline.visible = false;
					_view.eyes.visible = true;
					_view.eyes.addEventListener(MouseEvent.ROLL_OVER, onOver);
				}})
			}});
			
			TweenMax.killTweensOf(_view.eyes);
			_view.eyes.scaleX = _view.eyes.scaleY = 1;
			
				
		}
		
		public function get view():MovieClip {
			return _view;
		}
		
		public function get id():int {
			return _id;
		}
		
		public function set mode(val:int):void {
			
		}
		
		public function reset():void {
			_view.visible = true;
			_view.alpha = 1;
		}
	}
}